import { CreationOptional, InferAttributes, InferCreationAttributes } from 'sequelize'
import {
    AllowNull,
    AutoIncrement,
    Column,
    CreatedAt,
    DataType,
    Default,
    DeletedAt,
    Index,
    Model,
    PrimaryKey,
    Table,
    Unique,
    UpdatedAt,
} from 'sequelize-typescript'
import { AccountType } from '~/constants/AccountType'

/**
 * 账号表实体
 */
@Table({ freezeTableName: true, paranoid: true })
export class Account extends Model<InferAttributes<Account>, InferCreationAttributes<Account>> {
    /**
     * 自增id
     */
    @PrimaryKey
    @AutoIncrement
    @Column(DataType.INTEGER({ unsigned: true }))
    declare id: CreationOptional<number>

    /**
     * 账号appid
     */
    @Unique('account_component')
    @AllowNull(false)
    @Column(DataType.STRING(50))
    declare app_id: string

    /**
     * 托管到的第三方平台id（如果有）
     */
    @Unique('account_component')
    @Default('')
    @AllowNull(false)
    @Column(DataType.STRING(50))
    declare component_app_id: CreationOptional<string>

    /**
     * 账号类型
     */
    @Index
    @AllowNull(false)
    @Column(DataType.STRING(50))
    declare type: AccountType

    /**
     * 账号名称
     */
    @Column(DataType.STRING(50))
    declare name: CreationOptional<string | null>

    /**
     * 账号密钥
     */
    @Column(DataType.STRING(100))
    declare secret: CreationOptional<string | null>

    /**
     * 消息校验token
     */
    @Column(DataType.STRING(100))
    declare msg_token: CreationOptional<string | null>

    /**
     * 消息加解密密钥
     */
    @Column(DataType.STRING(43))
    declare encoding_aes_key: CreationOptional<string | null>

    /**
     * 账号扩展信息
     */
    @Column(DataType.JSON)
    declare extra: CreationOptional<any>

    /**
     * 创建时间
     */
    @CreatedAt
    @AllowNull(false)
    @Column(DataType.DATE)
    declare create_time: CreationOptional<Date>

    /**
     * 更新时间
     */
    @UpdatedAt
    @AllowNull(false)
    @Column(DataType.DATE)
    declare update_time: CreationOptional<Date>

    @DeletedAt
    @Column(DataType.DATE)
    declare delete_time: CreationOptional<Date | null>
}
